//
//  UIViewController+HUD.m
//  YiShou
//
//  Created by Kerry on 16/3/4.
//  Copyright © 2016年 FuMi. All rights reserved.
//

#import "UIViewController+HUD.h"

#import "MBProgressHUD.h"

@implementation UIViewController (HUD)

- (void)showLoadingHud{
    [self hideHud];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
}

- (void)showTipHudWithText:(NSString *)text
{
    [self showTipHudWithText:text andWithHUDMode:MBProgressHUDModeText andWithDuration:1.5 WithCompleteBlock:nil];
}

- (void)showAlwaysTipHudWithText:(NSString *)text
{
    [self showTipHudWithText:text andWithHUDMode:MBProgressHUDModeIndeterminate andWithDuration:0 WithCompleteBlock:nil];
}

- (void)showTipHudWithText:(NSString *)text withDuration:(NSTimeInterval)duration withCompleteBlock:(void (^)(void))block
{
    [self showTipHudWithText:text andWithHUDMode:MBProgressHUDModeText andWithDuration:duration WithCompleteBlock:block];
}

- (void)showTextHud:(NSString *)text withDuration:(NSTimeInterval)duration withCompleteBlock:(void (^)(void))block
{
    [self showTipHudWithText:text andWithHUDMode:MBProgressHUDModeIndeterminate andWithDuration:duration WithCompleteBlock:block];
}

- (void)showTipHudWithText:(NSString *)text andWithHUDMode:(MBProgressHUDMode)HUDMode andWithDuration:(NSTimeInterval)duration WithCompleteBlock:(void(^)(void))block
{
    [self hideHud];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = HUDMode;
    hud.labelText = text;
    hud.removeFromSuperViewOnHide = YES;
    
    if (duration > 0) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self hideHud];
            if (block) {
                block();
            }
        });
    }
}

- (void)hideHud{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}

@end
